package com.meihua.baoleyi.base;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.BannerBean;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.net.OnLoadMoreListener;
import com.meihua.baoleyi.net.OnRefreshListener;
import com.meihua.baoleyi.view.IRecyclerView;
import com.meihua.baoleyi.view.LoadMoreFooterView;
import com.meihua.baoleyi.widget.GlideImageLoader;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;

/**
 * Created by ${zhangyuanchao} on 2017/9/27.
 */

public abstract class BaseRecyclerViewBannerFragment extends BaseFragment {
    public LoadMoreFooterView mLoadMoreFooterView;
    private RecyclerView.Adapter mAdapter;
    private int mPage;
    private Banner banner;
    private IRecyclerView recyclerView;

    /*初始化irecyclerview*/
    public void initRecyclerView(final IRecyclerView recyclerView, RecyclerView.LayoutManager layoutManager, boolean isSowBanner, boolean isShowText) {
        this.recyclerView= recyclerView;
        recyclerView.setLayoutManager(layoutManager);
        /*binnerview*/
        if (isSowBanner && banner == null) {
            LinearLayout bannerView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.layout_banner_view, null);
            banner = bannerView.findViewById(R.id.banner);
            /*设置在首页显示问题*/
            bannerView.findViewById(R.id.banner_tv).setVisibility(isShowText ? View.VISIBLE : View.GONE);
            recyclerView.addHeaderView(bannerView);
        }
        /*加载更多底部View*/
        mLoadMoreFooterView = (LoadMoreFooterView) recyclerView.getLoadMoreFooterView();
        if (mAdapter == null) {
            mAdapter = getAdapter();
            recyclerView.setIAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        recyclerView.setOnRefreshListener(onRefreshListener);
        recyclerView.setOnLoadMoreListener(onLoadMoreListener);
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                getServiceData(1);
            }
        });
    }

    /*设置binnerView*/
    public void setBannerView(String url, boolean equals) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("cate_id",equals?"1":"3");//cate_id	是	int	1代表首页，3代表商城，7代表儿推（1张，非轮播图）
        NetHelper.get(url,treeMap , true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, okhttp3.Response response) {
                BannerBean bean = NetHelper.getBeanForJson(BannerBean.class,s);
                if (bean == null) return;
                if(bean.getCode().equals("100")) {
                    //设置图片加载器
                    banner.setImageLoader(new GlideImageLoader());
                    List<String> imgs = new ArrayList<String>();
                    for (BannerBean.DataBean dataBean : bean.getData()) {
                        imgs.add(dataBean.getSlide_img());
                    }
                    System.out.println("xxx"+imgs);
                    //设置图片集合
                    banner.setImages(imgs);
                    //banner设置方法全部调用完毕时最后调用
                    banner.start();
                }
            }
        });
    }

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract void getServiceData(int mPage);

    private OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore(View loadMoreView) {
            mPage++;
            getServiceData(mPage);
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
        }
    };

    private OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPage = 1;
            recyclerView.setRefreshing(true);
            getServiceData(mPage);
            mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
        }
    };
}
